package com.ml.mall.api.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.sys.SysSubscriptionEditDto;
import com.ml.mall.bean.req.sys.SysSubscriptionNewDto;
import com.ml.mall.bean.req.sys.SysSubscriptionQueryPageDto;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.consts.PermissionConst;
import com.ml.mall.entity.sys.SysSubscription;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.service.sys.SysSubscriptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @description 配套接口
 * @author yx
 * @date 2021/9/15
 * @version 1.0
*/
@Api(tags = "【auth】配套")
@RestController
@RequestMapping("/subscription")
public class SysSubscriptionController {
    @Autowired
    private SysSubscriptionService subscriptionService;

    @ApiOperation("分页查询配套")
    @PostMapping("/query_page")
//    @RequiresPermissions(PermissionConst.SUBSCRIPTION_QUERY_PAGE)
    public ApiRestResult subscriptionQueryPage(@RequestBody SysSubscriptionQueryPageDto dto){
        IPage<SysSubscription> result = subscriptionService.subscriptionQueryPage(dto);
        if(null == result){
            throw new BizException(CommonEnum.SELECT_ERROR);
        }
        return ApiRestResult.ok(result);
    }

    @ApiOperation("添加配套")
    @PostMapping("/new")
//    @RequiresPermissions(PermissionConst.SUBSCRIPTION_NEW)
    public ApiRestResult subscriptionNew(@RequestBody @Valid SysSubscriptionNewDto dto){
        Integer result = subscriptionService.subscriptionNew(dto);
        if(CommonConsts.FIXED_NUM_ZERO.equals(result)){
            throw new BizException(CommonEnum.INSERT_ERROR);
        }
        return ApiRestResult.ok(result);
    }

    @ApiOperation("编辑配套")
    @PostMapping("/edit")
//    @RequiresPermissions(PermissionConst.SUBSCRIPTION_EDIT)
    public ApiRestResult subscriptionEdit(@RequestBody @Valid SysSubscriptionEditDto dto){
        Integer result = subscriptionService.subscriptionEdit(dto);
        if(CommonConsts.FIXED_NUM_ZERO.equals(result)){
            throw new BizException(CommonEnum.UPDATE_ERROR);
        }
        return ApiRestResult.ok(result);
    }
}
